package com.hc.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.hc.api.utils.ApiUtil;
import com.hc.comm.base.Model;
import com.hc.comm.dto.UserdataDTO;
import com.hc.comm.exception.OprationStatus;
import com.hc.service.model.UserdataModel;
import com.hc.service.service.IUserdataService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hc.comm.base.BaseController;
import com.hc.comm.dto.AppManagerDTO;
import com.hc.comm.model.ResultModel;
import com.hc.comm.utils.ExceptionUtils;
import com.hc.comm.utils.JsonUtils;
import com.hc.comm.utils.StringUtil;
import com.hc.service.model.UserModel;
import com.hc.service.service.IUserService;

@Controller
@Scope("prototype")
@RequestMapping("/" + ApiUtil.ApiVersion.V1 + "/user/")
public class UserController extends BaseController {

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name="userdataService")
	private IUserdataService userdataService;

	@ResponseBody
	@RequestMapping("login")
	public ResultModel login(@ModelAttribute AppManagerDTO dto) throws Exception {
		if (dto == null || !StringUtil.isNotEmpty(dto.getToken())) {
			ExceptionUtils.throwBaseException("参数异常");
		}

		UserModel model = userService.getOne(dto);
		
		Map<String, UserModel> resultMap = new HashMap<String, UserModel>();
		resultMap.put("user", model);

		return JsonUtils.toResult(resultMap);
	}

	/**
	 *
	 * @throws Exception
	 * id:用户id
	 */
	@RequestMapping("personaldata")
	public void personaldata()throws Exception{
		String id=request.getParameter("id");
		System.out.print(id);
		//String access_token = request.getParameter("access_token");
		if (StringUtil.isEmpty(id)/*||StringUtil.isEmpty(access_token)*/){
			JsonUtils.toErrorResult(request, response, "参数不完整！");
			return;
		}
			UserdataDTO dto=new UserdataDTO();

			dto.setId(Long.valueOf(id));
			UserdataModel model=userdataService.getOne(dto);
//		if (model == null || !access_token.equals(model.getAppToken())) {
//			JsonUtils.toErrorResult(request, response, "请先登录!", OprationStatus.LOGIN.getCode());
//			return;
//		}
			JsonUtils.toSuccessResult(request, response,model, "userName","nickName","gender","birthday","portrait","address");

	}

	@RequestMapping("updatePersonalData")
	public void updatePersonalData()throws Exception{
		String id=request.getParameter("id");
		String nickName=request.getParameter("nickName");
		String birthday=request.getParameter("birthday");
		String gender=request.getParameter("gender");
		String address=request.getParameter("address");
		String portrait=request.getParameter("portrait");
		if (StringUtil.isEmpty(id)){
			JsonUtils.toErrorResult(request, response, "参数不完整！");
			return;
		}
		UserdataDTO dto=new UserdataDTO();
		dto.setId(Long.valueOf(id));
		UserdataModel model=userdataService.getOne(dto);
	if (model==null){
		JsonUtils.toSuccessResult(request, response,"用户不存在","");
		return;
	}else {
		if (!StringUtil.isEmpty(gender)){
			model.setGender(Integer.valueOf(gender));
		}
		if (!StringUtil.isEmpty(address)){
			model.setAddress(address);
		}
		if (!StringUtil.isEmpty(nickName)){
			model.setNickName(nickName);
		}
		if (!StringUtil.isEmpty(birthday)){
			model.setBirthday(birthday);
		}
		if (!StringUtil.isEmpty(portrait)){
			model.setPortrait(portrait);
		}
		userdataService.updateByPrimaryKey(model);
		JsonUtils.toSuccessResult(request, response, "success", "");
		}
	}

}
